﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Global;
using CE.Provider;
using CE.Database;
using CE.Security;

public partial class admin_user_Access_ProviderUserType : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/Admin/AdminSecureLogin.aspx");
        }

        if (!IsPostBack)
        {
            ClearForm();


            if (Request.Params["CUID"] != null && Request.Params["PCID"] != null)
            {
                Int64 _ceid, _pcid;
                if (Int64.TryParse(Request.Params["CUID"].ToString(), out _ceid) && Int64.TryParse(Request.Params["PCID"].ToString(), out _pcid))
                {
                    ddlProviderCompany.Text = _pcid.ToString();
                    LoadUserList();
                    ddlProviderUser.Text = _ceid.ToString();
                    ddlProviderCompany.Enabled = false;
                    ddlProviderUser.Enabled = false;
                    ShowUserType(_pcid, _ceid);
                }
            }
        }
    }

    protected void ddlProviderCompany_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadUserList();
        ddlUserType.SelectedIndex = 0;
        chkStatus.Checked = true;
        lblMessage.Text = "";
    }

    protected void ddlProviderUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlUserType.SelectedIndex = 0;
        if (ddlProviderCompany.Text != "0" && ddlProviderUser.Text != "0")
        {
            ShowUserType(Convert.ToInt64(ddlProviderCompany.Text), Convert.ToInt64(ddlProviderUser.Text));
        }
    }

    protected void btnView_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/User/Access/ViewProviderUserType.aspx");
    }

    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        ClearForm();
        LoadUserList();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            UserType _usertype = new UserType();
            UserTypeDB _usertypedb = new UserTypeDB();

            _usertype.ProviderCompanyId = Convert.ToInt64(ddlProviderCompany.Text);
            _usertype.CEID = Convert.ToInt64(ddlProviderUser.Text);
            _usertype.ProviderUserTypeId = Convert.ToInt64(ddlUserType.Text);
            _usertype.Status = chkStatus.Checked;
            _usertype.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());

            Int64 _result = _usertypedb.Insert(_usertype);

            if (_result > 0)
            {
                ClearForm();
                if (btnSubmit.Text.ToUpper() == "UPDATE")
                {
                    lblMessage.Text = "Update successfully!";
                }
                else
                {
                    lblMessage.Text = "Insert successfully!";
                }
            }
            else
            {
                if (btnSubmit.Text.ToUpper() == "UPDATE")
                {
                    lblMessage.Text = "Update failed!";
                }
                else
                {
                    lblMessage.Text = "Insert failed!";
                }
            }
        }
    }

    private void ShowUserType(Int64 ProviderCompanyId, Int64 CEID)
    {
        UserTypeDB _usertypedb = new UserTypeDB();

        DataTable _usertype = _usertypedb.GetProviderUserType(ProviderCompanyId, CEID);

        if (_usertype != null && _usertype.Rows.Count > 0)
        {
            ddlUserType.Text = _usertype.Rows[0]["ProviderUserTypeId"].ToString();
            chkStatus.Checked = Convert.ToBoolean(_usertype.Rows[0]["Status"].ToString());
        }
    }

    private void LoadUserList()
    {
        Int64 _ProviderCompanyId = Convert.ToInt64(ddlProviderCompany.Text);
        MyDatabase _db = new MyDatabase();
        DataTable _dtUser = new DataTable();
        SortedList _sPara = new SortedList();
        _sPara.Add("@ProviderCompanyId", _ProviderCompanyId);
        _dtUser = _db.GetDataTableBySP("sprocProviderUserSelectItemList", _sPara);

        if (_dtUser != null && _dtUser.Rows.Count > 0)
        {
            ddlProviderUser.Items.Clear();
            for (int i = 0; i < _dtUser.Rows.Count; i++)
            {
                ddlProviderUser.Items.Add(new ListItem(_dtUser.Rows[i]["Email"].ToString(), _dtUser.Rows[i]["CEID"].ToString()));
            }

            ddlProviderUser.Items.Insert(0, new ListItem("Select", "0"));
        }
        else
        {
            ddlProviderUser.Items.Clear();
            ddlProviderUser.Items.Insert(0, new ListItem("None", "0"));
        }
    }

    private void ClearForm()
    {
        lblMessage.Text = "";
        lblUserId.Text = "0";
        ddlUserType.Text = "-1";
        ddlProviderCompany.Text = "0";
        chkStatus.Checked = true;
        btnSubmit.Text = "Submit";
        //ddlProviderUser.Items.Clear();
        //ddlProviderUser.Items.Insert(0, new ListItem("None", "0"));
        ddlProviderCompany.Enabled = true;
        ddlProviderUser.Enabled = true;
    }

}
